/**
 * Filename: GuideActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/9 4:18
 */
package com.github.postsanf.yinian.guide;

import android.animation.ArgbEvaluator;
import android.annotation.TargetApi;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

import com.github.postsanf.yinian.BaseFragmentActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.LoginActivity;
import com.github.postsanf.yinian.activity.LoginWeChatActivity;
import com.github.postsanf.yinian.view.CirclePageIndicator;
import com.umeng.analytics.MobclickAgent;

public class GuideActivity extends BaseFragmentActivity implements View.OnClickListener {

    final float PARALLAX_COEFFICIENT = 1.2f;
    final float DISTANCE_COEFFICIENT = 0.5f;

    //	TextSwitcher mTextSwitcher;
    Button btn_guide_start;
    ViewPager mPager;
    CirclePageIndicator mPagerIndicator;

    FragmentAdapter mAdapter;

    SparseArray<int[]> mLayoutViewIdsMap = new SparseArray<int[]>();

    private void addGuide(BaseGuideFragment fragment) {
        mAdapter.addItem(fragment);
        mLayoutViewIdsMap.put(fragment.getRootViewId(), fragment.getChildViewIds());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_guide_fragment);
//		mTextSwitcher = (TextSwitcher) findViewById(R.id.tip);
        btn_guide_start = (Button) findViewById(R.id.btn_guide_start);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerIndicator = (CirclePageIndicator) findViewById(R.id.indicator);

        mAdapter = new FragmentAdapter(getSupportFragmentManager());
        addGuide(new FirstGuideFragment());
        addGuide(new SecondGuideFragment());
        addGuide(new ThirdGuideFragment());
        addGuide(new ForthGuideFragment());
        btn_guide_start.setOnClickListener(this);
        mPager.setAdapter(mAdapter);
        mPagerIndicator.setViewPager(mPager);

        mPager.setPageTransformer(true, new ParallaxTransformer(PARALLAX_COEFFICIENT, DISTANCE_COEFFICIENT));
        mPagerIndicator.setOnPageChangeListener(new GuidePageChangeListener());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_guide_start:
                // 记录引导页状态
                SharedPreferences.Editor ed = application.getSp_data().edit();
                ed.putBoolean(getString(R.string.SHOW_GUIDEPAGE), false);
                ed.commit();
                intent2Activity(LoginActivity.class);
                intent2Activity(LoginWeChatActivity.class);
                finish();
                break;
            default:
                break;
        }
    }

    class ParallaxTransformer implements ViewPager.PageTransformer {

        float parallaxCoefficient;
        float distanceCoefficient;

        public ParallaxTransformer(float parallaxCoefficient, float distanceCoefficient) {
            this.parallaxCoefficient = parallaxCoefficient;
            this.distanceCoefficient = distanceCoefficient;
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void transformPage(View page, float position) {
            float scrollXOffset = page.getWidth() * parallaxCoefficient;

            ViewGroup pageViewWrapper = (ViewGroup) page;
            @SuppressWarnings("SuspiciousMethodCalls")
            int[] layer = mLayoutViewIdsMap.get(pageViewWrapper.getId());

            for (int id : layer) {
                View view = page.findViewById(id);
                if (view != null) {
                    view.setTranslationX(scrollXOffset * position);
                }
                scrollXOffset *= distanceCoefficient;
            }
        }
    }

    class GuidePageChangeListener implements ViewPager.OnPageChangeListener {

        ArgbEvaluator mColorEvaluator;

        int mPageWidth, mTotalScrollWidth;

        int mGuideStartBackgroundColor, mGuideEndBackgroundColor;

        String[] mGuideTips;

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public GuidePageChangeListener() {
            mColorEvaluator = new ArgbEvaluator();

            mPageWidth = getWindowManager().getDefaultDisplay().getWidth();
            mTotalScrollWidth = mPageWidth * mAdapter.getCount();

            mGuideStartBackgroundColor = getResources().getColor(R.color.guide_start_background);
            mGuideEndBackgroundColor = getResources().getColor(R.color.guide_end_background);

//			mGuideTips = getResources().getStringArray(R.array.array_guide_tips);
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            float ratio = (mPageWidth * position + positionOffsetPixels) / (float) mTotalScrollWidth;
            Integer color = (Integer) mColorEvaluator.evaluate(ratio, mGuideStartBackgroundColor, mGuideEndBackgroundColor);
            mPager.setBackgroundColor(color);
        }

        @Override
        public void onPageSelected(int position) {
//			if (mGuideTips != null && mGuideTips.length > position) {
//				mTextSwitcher.setText(mGuideTips[position]);
//			}
            if (position == 3) {
                Animation animation = AnimationUtils.loadAnimation(GuideActivity.this, R.anim.yn_guide_items);
                btn_guide_start.setAnimation(animation);
                btn_guide_start.setVisibility(View.VISIBLE);
            } else {
                Animation animation = AnimationUtils.loadAnimation(GuideActivity.this, R.anim.yn_guide_items_hide);
                btn_guide_start.setAnimation(animation);
                btn_guide_start.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }


    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
